package test2;

public class Printer {
    int i = 0;

    public synchronized void print0() {
        while (i != 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(Thread.currentThread().getName() + "打印" + i);
        i++;
        notifyAll();
    }

    public synchronized void print1() {
        while (i != 1) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(Thread.currentThread().getName() + "打印" + i);
        i--;
        notifyAll();
    }
}
